package cn.itcast;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
@WebServlet("/addstudent")
public class AddStudent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");

        String age = req.getParameter("age");
        String score = req.getParameter("score");
        System.out.println(username);
        System.out.println(age);
        System.out.println(score);
        Student s = new Student(username,Integer.parseInt(age),Integer.parseInt(score));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\stu.txt",true));
        bw.write(s.toString());
        bw.newLine();
        bw.close();
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("保存成功,2秒后自动跳转首页");
        resp.setHeader("refresh","2;url=/stu4.6/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
